package com.huangyi.ack.controller;

import com.huangyi.ack.constants.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/ack")
public class ProducerController {
    @Resource(name = "returnRabbitTemplate")
    RabbitTemplate returnRabbitTemplate;
    @Resource(name = "rabbitTemplate")
    RabbitTemplate rabbitTemplate;
    @Resource(name = "confirmRabbitTemplate")
    RabbitTemplate confirmRabbitTemplate;

    @RequestMapping("/auto")
    public String auto() {
        for (int i = 0; i < 1; i++) {
            rabbitTemplate.convertAndSend(Constants.AKC_EXCHANGE_AUTO,"abc1","auto 发送消息：" + i);
        }
        return "auto success";
    }

    @RequestMapping("/none")
    public String none() {
        for (int i = 0; i < 1; i++) {
            rabbitTemplate.convertAndSend(Constants.AKC_EXCHANGE_NONE,"abc","none 发送消息：" + i);
        }
        return "none success";
    }

    @RequestMapping("/manual")
    public String manual() {
        for (int i = 0; i < 1; i++) {
            rabbitTemplate.convertAndSend(Constants.AKC_EXCHANGE_MANUAL,"abc","manual 发送消息：" + i);
        }
        return "manual success";
    }
    @RequestMapping("/confirm")
    public String confirm() {
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"abc","confirm.test",correlationData);
        return "confirm success";
    }
    @RequestMapping("/confirmFalse")
    public String confirmFalse() {
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"abc11","confirm.test",correlationData);
        return "confirm success";
    }

    @RequestMapping("/return")
    public String returnMassage(){
        CorrelationData correlationData = new CorrelationData("1");
        returnRabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"abc11","confirm.test",correlationData);
        return "returnMassage success";
    }

    @RequestMapping("/retryAuto")
    public String retryAuto() {
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"abc","retry auto",correlationData);
        return "retryAuto success";
    }

    @RequestMapping("/retryManual")
    public String retryManual() {
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"abc","retry manual",correlationData);
        return "retryManual success";
    }

    @RequestMapping("/ttlMessage")
    public String ttlMessage(){
        System.out.println("ttl......");
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        };
        MessagePostProcessor messagePostProcessor2 = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("4000");
                return message;
            }
        };
        System.out.println("ttl2 .....");
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl1",messagePostProcessor);
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl2",messagePostProcessor2);
//        confirmRabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","confirm2.test");
        return "ttlMessage success";
    }

    public String ttlQueue(){
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        };
        return "ttlMessage success";
    }
    @RequestMapping("/dl")
    public String dl() {
        confirmRabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","dl");
        return "dl success";
    }

    @RequestMapping("/test")
    public String test() {
        rabbitTemplate.convertAndSend(Constants.TEST_EXCHANGE,"abc","test");
        return "test success";
    }
//    @RequestMapping("/confirm1")
//    public String confirm(@Autowired ConnectionFactory connectionFactory) {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean b, String s) {
//                System.out.println("Confirm 生产端发送成功" );
//                if(b){
//                    System.out.printf("Exchange 接收到消息 ，消息ID: %s \n" , correlationData == null? null : correlationData.getId());
//                }else {
//                    System.out.printf("Exchange 未接收到消息，消息ID:%s ， cause: %s\n",correlationData == null? null : correlationData.getId()
//                            ,s);
//                }
//            }
//        });
//        CorrelationData correlationData = new CorrelationData("1");
//        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"abc","confirm.test",correlationData);
//        return "confirm success";
//    }
}

